package myReflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Reflect01 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<?> clazz =Student.class;
        System.out.println(clazz);
        //获取public构造
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
        System.out.println();
        //获取所有构造
        Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
        //获取指定参数public构造
        Constructor<?> constructor = clazz.getConstructor();
        System.out.println(constructor);
        //获取指定参数构造
        Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(int.class);
        System.out.println(declaredConstructor);
        //以上获取到了protected构造 需暴力反射才能使用创建对象
        int modifiers = declaredConstructor.getModifiers();
        System.out.println(modifiers);
        declaredConstructor.setAccessible(true);
        Student student = (Student) declaredConstructor.newInstance(15);
        System.out.println(student);
    }
}
